package com.lei.study.mvel.high;

import org.mvel2.MVEL;
import org.mvel2.templates.TemplateRuntime;

import java.util.HashMap;
import java.util.Map;

/**
 * 精度处理
 * @author 伍磊
 */
public class Jd {

    public static void main(String[] args) {
        add();
        add2();
    }

    private static void add() {
        String expression = "def calc() { return currentRate / historyRate; } return calc();";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("currentRate", 0.01800000);
        paramMap.put("historyRate", 0.01300000);
        /**匹配表达式*/
        expression = (String) TemplateRuntime.eval(expression, paramMap);
        Object object = MVEL.eval(expression, paramMap);
        System.out.println("输出结果：" + object);
    }

    private static void add2() {
        String expression = "def calc() { return @{currentRate}B / @{historyRate}B; } ";
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("currentRate", 0.01800000);
        paramMap.put("historyRate", 0.01300000);
        /**匹配表达式*/
        expression = (String) TemplateRuntime.eval(expression, paramMap);
        Object object = MVEL.eval(expression, paramMap);
        System.out.println("输出结果：" + object);
    }
}
